﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Security.AccessControl;

namespace ImportSales.Ftp
{
    public class FtpProcess
    {        
        public string FTPServer { get; set; }

        public string FTPUser { get; set; }

        public string FTPPassword { get; set; }

        public List<string> GetFilesFromDirectory(string directory)
        {
            List<string> filesInDirectory = new List<string>();
            FtpWebRequest fwr = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPServer + directory));
            fwr.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            fwr.Credentials = new NetworkCredential(FTPUser, FTPPassword);

            using (FtpWebResponse response = (FtpWebResponse)fwr.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        string line;                        

                        while ((line = reader.ReadLine()) != null)
                        {
                            int lastWhiteSpaceIndex = line.LastIndexOf(' ');
                            line = line.Substring(lastWhiteSpaceIndex + 1, line.Length - lastWhiteSpaceIndex -1);
                            filesInDirectory.Add(line);
                        }

                        reader.Close();
                        response.Close();
                    }
                }
            }

            return filesInDirectory;
        }

        public void DownLoadFile(string fileInFTP, string destinationPath)
        {
            FtpWebRequest fwr = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPServer + fileInFTP));
            fwr.Method = WebRequestMethods.Ftp.DownloadFile;

            fwr.Credentials = new NetworkCredential(FTPUser, FTPPassword);

            using (FtpWebResponse response = (FtpWebResponse)fwr.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {                           
                    string justDirectory = Path.GetDirectoryName(destinationPath);
                    if (!Directory.Exists(justDirectory))
                    {
                        Directory.CreateDirectory(justDirectory);
                    }

                    using (FileStream fileStream = File.Create(destinationPath))
                    {
                        responseStream.CopyTo(fileStream);
                        responseStream.Close();
                        response.Close();
                    }
                }
            }
        }

        public void DeleteFile(string fileInFTP)
        {
            FtpWebRequest fwr = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTPServer + fileInFTP));
            fwr.Method = WebRequestMethods.Ftp.DeleteFile;

            fwr.Credentials = new NetworkCredential(FTPUser, FTPPassword);

            using (FtpWebResponse response = (FtpWebResponse)fwr.GetResponse())
            {
                response.Close();
            }

        }
    }
}
